<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>登录</span>
      </div>
      <el-form
        label-position="left"
        :rules="rules"
        ref="loginForm"
        :model="form"
        label-width="60px"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="el-icon-s-goods"
            placeholder="请输入密码"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox label="记住密码" v-model="form.rem"></el-checkbox>
        </el-form-item>
        <el-button-group>
          <el-button type="primary" @click="login" :loading="isLoading"
            >登录</el-button
          >
          <el-button type="primary" @click="reset">重置</el-button>
        </el-button-group>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { api_login } from '../api/admin.js'

export default {
  data() {
    return {
      isLoading: false,
      form: {
        username: '',
        password: '',
        rem: false
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.isLoading = true
      //1.验证表单
      this.$refs.loginForm.validate(async valid => {
        if (!valid) {
          console.log('验证失败')
          return false
        }
        //2.提交表单
        const { data: res } = await api_login(
          this.form.username,
          this.form.password
        )
        this.isLoading = false
        if (res.code !== 200) {
          this.$message.error(res.message)
          return true
        }
        const data = res.data
        const token = data.tokenHead + data.token
        //3.保存token到sessionStorage
        window.sessionStorage.setItem('token', token) //会话结束后，自动消除
        //4.保存用户信息到vuex中的store
        //5.记住密码功能，登录将用户密码保存到cookies
        if (this.form.rem) {
          this.$cookies.set('username', this.form.username, '7D') //默认是一天过期
          this.$cookies.set('password', this.form.password, '7D')
        } else {
          this.$cookies.remove('username')
          this.$cookies.remove('password')
        }
        //6.登录页面刷新或重新加载的时候从cookies中获取，在create()函数中
        //7.跳转到主页面，编程式路由跳转
        await this.$router.replace('/home')
      })
    },
    reset() {
      this.$refs['loginForm'].resetFields()
    }
  },
  created() {
    //获取cookies中的值
    this.form.username = this.$cookies.get('username')
    this.form.password = this.$cookies.get('password')
  }
}
</script>

<style scoped>
/*scoped：表示该组件中的样式只是当前组件中有效*/
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
  margin: auto;
  transform: translateY(50%);
}

.el-button-group {
  width: 100%;
}

.el-button-group > .el-button {
  width: 50%;
}
</style>
